package laba7.taskB;

import interfaces.task7.executor.Task;
import interfaces.task7.executor.TasksStorage;
import java.util.ArrayList;
import java.util.List;

/**
 * The class is a storage for tasks.
 *
 * @author yakimovich
 */
public class TasksStorageImpl implements TasksStorage {
	/**
	 * Container for Tasks.
	 */
	private List<Task> storage = new ArrayList<>();
	/**
	 * The number of Tasks in the storage.
	 */
	private int count;

	/**
	 * The method adds Task in the TasksStorage.
	 *
	 * @param task
	 *            Task for added
	 */
	@Override
	public synchronized void add(Task task) {
		if (task == null) {
			throw new NullPointerException();
		}
		storage.add(task);
		count++;
	}

	/**
	 * Method for extracting the Task from the TasksStorage.
	 *
	 * @return Task
	 */
	@Override
	public synchronized Task get() {
		if (count == 0) {
			return null;
		}
		Task result = storage.remove(0);
		count--;
		return result;
	}

	/**
	 * @return number of Tasks in the TasksStorage.
	 */
	@Override
	public int count() {
		return count;
	}
}
